import request from "@/utils/axios";
import {Schedule, QueryParams, ResponseData} from "./types"
import type { AxiosResponse } from "axios";

/**
 * 用于调用 API 接口的函数
 * @method SsoUserApi 获取用户相关
 */

export default {
    // 获取排班列表
    getSchedules(params?: QueryParams): Promise<AxiosResponse<ResponseData<Schedule[]>>> {
      return request({
        url: '/api/schedule',
        method: 'get',
        params
      })
    },
  
    // 获取单个排班详情
    getSchedule(timeId: string): Promise<AxiosResponse<ResponseData<Schedule>>> {
      return request({
        url: `/api/schedule/${timeId}`,
        method: 'get'
      })
    },
  
    // 创建单个排班
    createSchedule(data: Partial<Schedule>): Promise<AxiosResponse<ResponseData<Schedule>>> {
      return request({
        url: '/api/schedule',
        method: 'post',
        data
      })
    },
  
    // 批量创建排班
    createSchedules(scheduleList: Partial<Schedule>[]): Promise<AxiosResponse<ResponseData<Schedule>>> {
      return request({
        url: '/api/schedule',
        method: 'post',
        data: {
          ScheduleList: JSON.stringify(scheduleList)
        }
      })
    },
  
    // 更新排班信息
    updateSchedule(timeId: string, data: Partial<Schedule>): Promise<AxiosResponse<ResponseData<Schedule>>> {
      return request({
        url: `/api/schedule/${timeId}`,
        method: 'put',
        data
      })
    },
  
    // 敏感信息更新
    sensitiveUpdate(timeId: string, data: {
      period?: number
      status?: number
      is_delete?: number
    }): Promise<AxiosResponse<ResponseData<Schedule>>> {
      const formData = new FormData()
      Object.entries(data).forEach(([key, value]) => {
        if (value !== undefined) {
          formData.append(key, value.toString())
        }
      })
      return request({
        url: `/api/schedule/update/${timeId}`,
        method: 'put',
        data: formData
      })
    },
  
    // 删除排班
    deleteSchedule(timeId: string): Promise<AxiosResponse<ResponseData<any>>> {
      return request({
        url: `/api/schedule/${timeId}`,
        method: 'delete'
      })
    }
  }